import { createRouter, createWebHistory } from "vue-router"
import Login from "@/views/login/Login.vue"
import Dashboard from "@/views/workbench/Dashboard.vue"
import InventoryCount from "@/views/warehouse/InventoryCount.vue"
import OutboundOrder from "@/views/warehouse/OutboundOrder.vue"
import OutboundDetail from "@/views/warehouse/OutboundDetail.vue"
import SupplierManagement from "@/views/purchase/SupplierManagement.vue"
import PurchaseRequisition from "@/views/purchase/PurchaseDemand.vue";
import PurchaseStorage from "@/views/purchase/PurchaseStorage.vue";
import PurchaseDemand from "@/views/purchase/PurchaseDemand.vue";
import BOMManagement from '@/views/database/BOMManagement.vue'
import ProcessManagement from '@/views/database/ProcessManagement.vue'
import AIAssistant from '@/views/ai/AIAssistant.vue'
import AccountsReceivableDetail from '@/views/finance/AccountsReceivableDetail.vue'
import AccountsPayableDetail from '@/views/finance/AccountsPayableDetail.vue'
import Receipt from '@/views/finance/Receipt.vue'
import ReceiptWriteOff from '@/views/finance/ReceiptWriteOff.vue'
import PaymentWriteOff from '@/views/finance/PaymentWriteOff.vue'
import QuotationManagement from '@/views/sale/QuotationManagement.vue'
import DeliveryManagement from '@/views/sale/DeliveryManagement.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: Dashboard,
      meta: { requiresAuth: true }
    },
    {
      path: "/warehouse/inventory-count",
      name: "InventoryCount",
      component: InventoryCount,
      meta: { requiresAuth: true }
    },
    {
      path: "/warehouse/outbound-order",
      name: "OutboundOrder",
      component: OutboundOrder,
      meta: { requiresAuth: true }
    },
    {
      path: "/warehouse/outbound-detail",
      name: "OutboundDetail",
      component: OutboundDetail,
      meta: { requiresAuth: true }
    },
    {
      path: "/purchase/supplier",
      name: "SupplierManagement",
      component: SupplierManagement,
      meta: { requiresAuth: true }
    },
    {
      path: "/purchase/storage",
      name: "PurchaseStorage",
      component: PurchaseStorage,
      meta: { requiresAuth: true }
    },
    {
      path: "/produce/purchase-demand",
      name: "PurchaseDemand",
      component: PurchaseDemand,
      meta: { requiresAuth: true, title: "采购需求管理" }
    },
    {
      path: '/database/bom-management',
      name: 'BOMManagement',
      component: BOMManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/database/process-management',
      name: 'ProcessManagement',
      component: ProcessManagement,
      meta: { requiresAuth: true, title: '工序管理' }
    },
    {
      path: '/finance/receivable-detail',
      name: 'AccountsReceivableDetail',
      component: AccountsReceivableDetail,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/finance/payable-detail',
      name: 'AccountsPayableDetail',
      component: AccountsPayableDetail,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/finance/receipt',
      name: 'Receipt',
      component: Receipt,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/finance/receipt-write-off',
      name: 'ReceiptWriteOff',
      component: ReceiptWriteOff,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/sale/quotation',
      name: 'QuotationManagement',
      component: QuotationManagement,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/sale/delivery',
      name: 'DeliveryManagement',
      component: DeliveryManagement,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/finance/payment-write-off',
      name: 'PaymentWriteOff',
      component: PaymentWriteOff,
      meta: { requiresAuth: true }
    }
    ,{
      path: '/finance/payment-verification',
      name: 'PaymentVerificationOrder',
      component: PaymentWriteOff,
      meta: { requiresAuth: true }
    }
    ,
    {
      path: '/ai',
      name: 'AIAssistant',
      component: AIAssistant,
      meta: { requiresAuth: true, title: 'AI 助手' }
    }
  ],
})

// 读取 token（localStorage 优先，兼容 sessionStorage）
function getToken() {
  return localStorage.getItem('token') || sessionStorage.getItem('token')
}

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (to.path === '/login' && token) {
    // 已登录访问登录页，跳首页
    return next('/dashboard')
  }
  if (to.meta?.requiresAuth && !token) {
    return next({ path: '/login', query: { redirect: to.fullPath } })
  }
  next()
})

export default router
